/**
 * 格式化时间
 * @param {number} agoTime
 * @returns {string}
 */
const formatTime = (agoTime) => {
    const diff = agoTime / 1000
    if (diff < 60) {
        return '刚刚'
    } else if (diff < 3600) {
        // less 1 hour
        return parseInt(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
        return parseInt(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 30) {
        return parseInt(diff / (3600 * 24)) + '天前'
    } else if (diff <= 3600 * 24 * 30 * 12) {
        return parseInt(diff / (3600 * 24 * 30)) + '个月前'
    } else {
        return parseInt(diff / (3600 * 24 * 30 * 12)) + '年月前'
    }
}


/**
 * 获取当前周开始结束时间
 * @returns {{sunday: number, monday: number}}
 */
const getBeginAndEndDateOfCurrent = ()=>{
    const now = new Date()
    //今天周几
    let currentDay = parseInt(now.getDay())
    //当前为周日
    if(currentDay === 0){
      currentDay = 7
    }
    //周一时间戳
    const mondayTime = now.getTime() - (currentDay - 1)*24*60*60*1000
    //周日时间戳
    const sundayTime = now.getTime() + (7 - currentDay)*24*60*60*1000
    const Monday = new Date(mondayTime).toLocaleDateString()
    const Sunday = new Date(sundayTime).toLocaleDateString()
    return {Monday:Monday,Sunday:Sunday}
}

/**
 * 获取指定时间戳的日期
 * @param timestamp
 * @returns {string}
 */
const getDateBySpecifyTimestamp = (timestamp)=>{
    const now = new Date(timestamp)
    const year = now.getFullYear()
    const month = now.getMonth() + 1
    const date = now.getDate()
    return year + '-' + month + '-' + date
}
/**
 * 获取当前周开始结束每一天日期
 * @param index
 * @returns {string}
 */
const getBeginToEndDateOfCurrentWeek = (index)=>{
    const now = new Date()
    const firstDay = new Date(now - (now.getDay() - 1)*86400000)
    firstDay.setDate(firstDay.getDate()+index)
    const month = Number(firstDay.getMonth())+1
    return now.getFullYear() + "/"+month +"/" + firstDay.getDate()
}

/**
 * 获取时间戳
 * @param {string} type
 * @returns {Date}
 */
const getTime = (type) =>{
    if (type === 'start') {
      return new Date().getTime() - 3600 * 1000 * 24 * 90
    } else {
      return new Date(new Date().toDateString())
    }
}
/**
 * 时间格式转化
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string}
 */
const parseTime = (time, cFormat) =>{
    if (arguments.length === 0) {
      return null
    }
    const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
    let date
    if (typeof time === 'object') {
      date = time
    } else {
      if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
        time = parseInt(time)
      }
      if ((typeof time === 'number') && (time.toString().length === 10)) {
        time = time * 1000
      }
      date = new Date(time)
    }
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }
    const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
      let value = formatObj[key]
      // Note: getDay() returns 0 on Sunday
      if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
      if (result.length > 0 && value < 10) {
        value = '0' + value
      }
      return value || 0
    })
    return time_str
}

/**
 * 根据当前时间动态显示时间
 * @param {number} time:
 * @param {string} option:
 * @returns {string}:
 */
const formatTime = (time, option) => {
    if (('' + time).length === 10) {
      time = parseInt(time) * 1000
    } else {
      time = +time
    }
    const d = new Date(time)
    const now = Date.now()
  
    const diff = (now - d) / 1000
  
    if (diff < 30) {
      return '刚刚'
    } else if (diff < 3600) {
      // less 1 hour
      return Math.ceil(diff / 60) + '分钟前'
    } else if (diff < 3600 * 24) {
      return Math.ceil(diff / 3600) + '小时前'
    } else if (diff < 3600 * 24 * 2) {
      return '1天前'
    }
    if (option) {
      return parseTime(time, option)
    } else {
      return (
        d.getMonth() +1 +'月' +
        d.getDate() +'日' +
        d.getHours() +'时' +
        d.getMinutes() +'分'
      )
    }
}

/**
 * 比较开始结束时间大小，开始时间不能比结束时间大
 * @param begin
 * @param end
 * @returns {boolean}
 */
const compareTime = (begin, end) => {
    const dateTime0 = new Date(begin.replace('-', '/'))
    const nowDate0 = new Date(end.replace('-', '/'))
    return dateTime0 > nowDate0
}

/**
 * 获取指定时间到00:00过了多少分钟
 * @param date
 * @returns {number}
 */
const getMinutesToZero = (datetime) => {
    let date = new Date(datetime)
    let hour = date.getHours()
    let minutes = date.getMinutes()
    return hour * 60 + minutes
}
  
/**
 * 获取开始结束时间的总分钟数
 * @param start
 * @param end
 * @returns {number}
 */
const getDuration = (start, end) => {
    let startTime = new Date(start)
    let endTime = new Date(end)
    let startHour = startTime.getHours()
    let startMinutes = startTime.getMinutes()
    let endHour = endTime.getHours()
    let endMinutes = endTime.getMinutes()
    return (endHour * 60 + endMinutes) - (startHour * 60 + startMinutes)
}

/**
 * 获取毫秒
 * @param date
 * @returns {number}
 */
const getMillise = (date) => {
  return Date.parse(date)
}
/**
 * 计算与当前日期相差天数
 * @param date
 * @returns {number}
 */
const remainDate = (date) => {
  var dateBe = new Date()
  var dateEn = new Date(date.replace(/-/g, '/'))
  var remain = dateEn.getTime() - dateBe.getTime()
  return Math.floor(remain / (24 * 3600 * 1000))
}
/**
 * 计算相差天数
 * @param date1
 * @param date2
 * @returns {number}
 */
const remainDate1 = (date1, date2) => {
  var dateBe = new Date()
  var dateEn = new Date()
  if (isNaN(Date.parse(date1)) && isNaN(Date.parse(date2))) {
    return 0
  } else if (isNaN(Date.parse(date1)) && !isNaN(Date.parse(date2))) {
    dateBe = new Date()
    dateEn = new Date(date2.replace(/-/g, '/'))
  } else if (!isNaN(Date.parse(date1)) && isNaN(Date.parse(date2))) {
    dateBe = new Date(date1.replace(/-/g, '/'))
    dateEn = new Date()
  } else {
    dateBe = new Date(date1.replace(/-/g, '/'))
    dateEn = new Date(date2.replace(/-/g, '/'))
  }
  var remain = dateEn.getTime() - dateBe.getTime()
  return Math.floor(remain / (24 * 3600 * 1000))
}
  